﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Data;
using System.IO;
using System.Configuration;
namespace eDocWS.Common
{
    public class Utils
    {
        public static string FolderXML = ConfigurationManager.AppSettings["FolderXml"].ToString();
        public static string DomainFile = ConfigurationManager.AppSettings["DomainFile"].ToString();
        public static string FolderPDF = ConfigurationManager.AppSettings["FolderPDF"].ToString();
        public static bool CheckAuthen = Convert.ToBoolean((ConfigurationManager.AppSettings["CheckAuthen"]));
        public static string GetFileInFolder(string path)
        {
            string result = "";
            return result;
        }

        public static DataTable SetTableFile()
        {
            DataTable dt = new DataTable();
            DataColumn dcFolder = new DataColumn("FolderName", typeof(String));
            dt.Columns.Add(dcFolder);

            DataColumn dcFileName = new DataColumn("FileName", typeof(String));
            dt.Columns.Add(dcFileName);

            DataColumn dcPathFile = new DataColumn("PathFile", typeof(String));
            dt.Columns.Add(dcPathFile);

            DataColumn dcDateCreated = new DataColumn("DateCreated", typeof(DateTime));
            dt.Columns.Add(dcDateCreated);

            DataColumn dcDateModify = new DataColumn("DateModify", typeof(DateTime));
            dt.Columns.Add(dcDateModify);

            DataColumn dcCover = new DataColumn("Cover", typeof(String));
            dt.Columns.Add(dcCover);

            dt.AcceptChanges();
            return dt;
        }

        public static IEnumerable<string> GetFiles(string path)
        {
            Queue<string> queue = new Queue<string>();
            queue.Enqueue(path);
            while (queue.Count > 0)
            {
                path = queue.Dequeue();
                try
                {

                    foreach (string subDir in Directory.GetDirectories(path))//.Where(s => s.EndsWith(".png") || s.EndsWith(".pdf")))
                    {
                        queue.Enqueue(subDir);
                    }
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex);
                }
                string[] files = null;
                try
                {
                    files = Directory.GetFiles(path, "*.pdf");
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex);
                }
                if (files != null)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        yield return files[i];
                    }
                }
            }
        }

        public static void CreateXml(DataTable dt, string pathXML)
        {

            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(docNode);

            XmlNode responseNode = doc.CreateElement("response");
            doc.AppendChild(responseNode);

            XmlNode listNode = doc.CreateElement("list");
            responseNode.AppendChild(listNode);



            string title = "", link = "", cover = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                XmlNode pdfNode = doc.CreateElement("pdf");
                listNode.AppendChild(pdfNode);
                title = dt.Rows[i]["FileName"].ToString();
                link = dt.Rows[i]["PathFile"].ToString();
                cover = dt.Rows[i]["Cover"].ToString();
                XmlNode titleNode = doc.CreateElement("title");
                titleNode.AppendChild(doc.CreateTextNode(title));
                pdfNode.AppendChild(titleNode);
                XmlNode linkNode = doc.CreateElement("link");
                linkNode.AppendChild(doc.CreateTextNode(link));
                pdfNode.AppendChild(linkNode);
                XmlNode coverNode = doc.CreateElement("cover");
                coverNode.AppendChild(doc.CreateTextNode(cover));
                pdfNode.AppendChild(coverNode);
            }            
            doc.Save(pathXML);
        }

        public static bool CheckFileExists(string pathFile)
        {
            bool result = true;

            result = File.Exists(pathFile);

            return result;
        }
    }
}